<template>
  <el-dialog title="检验依据审批" :visible.sync="isOpen" width="600px" append-to-body>
    <el-form ref="form" :model="auditForm" :rules="rules" label-width="120px">
      <el-form-item label="审批结论" prop="quality_standard_status">
        <!-- <el-select
          v-model="auditForm.quality_standard_status"
          placeholder="请选择审批结论"
          clearable
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="(item, index) in auditOptions"
            :key="index"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          />
        </el-select> -->
        <el-radio-group v-model="auditForm.quality_standard_status">
          <el-radio v-for="(item,index) in bottons" :key="index" :label="item.Type">{{ item.Name }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="审批意见" prop="idea">
        <el-input
          v-model="auditForm.idea"
          type="textarea"
          placeholder="请输入审批意见"
          :autosize="{ minRows: 3, maxRows: 6 }"
          :style="{ width: '100%' }"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  approval
} from '@/api/qualitySource'
export default {
  props: {
    id: {
      type: String,
      default: () => {}
    },
    auditData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      isOpen: false,
      user_id: '',
      bottons: [],
      auditForm: {
        quality_standard_status: 'Next',
        idea: ''
      },
      auditOptions: [
        {
          label: '通过',
          value: 3
        },
        {
          label: '不通过',
          value: -1
        },
        {
          label: '回退',
          value: -2
        }
      ],
      // 弹框
      rules: {
        quality_standard_status: [
          { required: true, message: '请选择评估结论', trigger: 'blur' }
        ],
        idea: []
      }
    }
  },
  // 生命周期 - 创建完成（访问当前this实例）
  created() {},
  methods: {
    // 表单重置
    reset() {
      this.auditForm = {
        quality_standard_status: 'Next',
        idea: ''
      }
      this.resetForm('form')
    },
    // 取消按钮
    close() {
      this.isOpen = false
      this.reset()
    },
    // 打开弹框
    open() {
      this.isOpen = true
      this.user_id = this.$store.getters.userInfo.user.id
      this.getButton()
    },
    /** 提交按钮 */
    submitForm() {
      const that = this
      this.$refs['form'].validate((valid) => {
        if (valid) {
          that.approvalOperation()
        }
      })
    },
    approvalOperation() {
      // let status = 3
      // if (this.auditForm.quality_standard_status === 'Next') {
      //   status = 3
      // } else if (this.auditForm.quality_standard_status === 'Refuse') {
      //   status = -1
      // } else if (this.auditForm.quality_standard_status === 'Reject') {
      //   status = -2
      // }
      const datas = { id: this.id, quality_standard_status: this.auditForm.quality_standard_status, idea: this.auditForm.idea }
      approval(datas).then(res => {
        if (res.code === 200) {
          this.$emit('auditSuccess')
          this.close()
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          })
        }
      })
    },
    getButton() {
      if (this.auditData.State !== 'Done' || this.auditData.State !== 'Refuse') {
        if (this.auditData.Next && this.auditData.Next.length) {
          for (let i = 0; i < this.auditData.Next.length; i++) {
            const data = this.auditData.Next[i]
            const index = data.Staffs.findIndex(a => a === this.user_id)

            if (index !== -1) {
              this.bottons = data.Buttons || []
              const inx = this.bottons.findIndex(a => a.Type === 'Cancel')
              if (inx !== -1) {
                this.bottons.splice(inx, 1)
              }
              break
            }
          }
        }
      }
      console.log(this.bottons)
    }
  }
}
</script>
<style scoped>
/* @import url(); 引入css类 */
</style>
